package com.campusflea;

/**
 * Created by Administrator on 2017/6/7.
 */

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import io.rong.imkit.RongIM;
import io.rong.imlib.RongIMClient;
import io.rong.imlib.model.UserInfo;

public class ChatActivity extends Activity implements View.OnClickListener, RongIM.UserInfoProvider {

    private static final String token1 = "i2Wcg/ojf34WweHoL7DLGYJHsQ4pY8J5TNCR+OC26eQDAx3rASkssPc2KrvwROWolSR6nBU9TRXX6SYNRytPpw==";
    private static final String token2 = "odzjYiWlGjM0J8g1NvBEtF7gKk+dyLWLBDrMUaMUBMOY+aQ1hVNZ2lybrCCVFuEy8sC90kfgtd38Z58BGcdSfw==";

    private List<Friend> userIdList;
    private static final String TAG = "ChatActivity";

    private Button mUser1, mUser2;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.chatlayout);

        mUser1 = (Button) findViewById(R.id.connect_10010);
        mUser2 = (Button) findViewById(R.id.connect_10086);
        mUser1.setOnClickListener(this);
        mUser2.setOnClickListener(this);


        initUserInfo();

    }

    private void connectRongServer(String token) {
        if (getApplicationInfo().packageName.equals(App.getCurProcessName(getApplicationContext()))) {
            Log.e("text","--------------1111111111111----------------");
            /**
             * IMKit SDK调用第二步,建立与服务器的连接
             */
            RongIMClient.connect(token, new RongIMClient.ConnectCallback() {


                /**
                 * Token 错误，在线上环境下主要是因为 Token 已经过期，您需要向 App Server 重新请求一个新的 Token
                 */
                @Override
                public void onTokenIncorrect() {

                    Log.d("LoginActivity", "--onTokenIncorrect");
                    Log.e("text","--------------222222222222222----------------");
                }

                /**
                 * 连接融云成功
                 * @param userid 当前 token
                 */
                @Override
                public void onSuccess(String userid) {

                    Log.d("LoginActivity", "--onSuccess---" + userid);
                    Log.e("text","--------------3333333333333----------------");
                    if (userid.equals("10010")) {
                        mUser1.setText("用户1连接服务器成功");
                        Intent intent= new Intent(ChatActivity.this, ConversationListActivity.class);
                        startActivity(intent);
                        Toast.makeText(ChatActivity.this, "connect server success 10010", Toast.LENGTH_SHORT).show();

                    } else {

//                        startActivity(new Intent(ChatActivity.this, HomeActivity.class));
                        Toast.makeText(ChatActivity.this, "connect server success 10086", Toast.LENGTH_SHORT).show();
                    }
                }

                /**
                 * 连接融云失败
                 * @param errorCode 错误码，可到官网 查看错误码对应的注释
                 */
                @Override
                public void onError(RongIMClient.ErrorCode errorCode) {

                    Log.d("LoginActivity", "--onError" + errorCode);
                }
            });
        }

    }


    @Override
    public void onClick(View v) {

        if (v.getId() == R.id.connect_10010) {
            connectRongServer(token1);
        } else if (v.getId() == R.id.connect_10086) {
            connectRongServer(token2);
        }
    }


    private void initUserInfo() {
        userIdList = new ArrayList<Friend>();
        userIdList.add(new Friend("10010", "联通", "http://www.51zxw.net/bbs/UploadFile/2013-4/201341122335711220.jpg"));//联通图标
        userIdList.add(new Friend("10086", "移动", "http://img02.tooopen.com/Download/2010/5/22/20100522103223994012.jpg"));//移动图标
        RongIM.setUserInfoProvider(this, true);
    }

    @Override
    public UserInfo getUserInfo(String s) {

        for (Friend i : userIdList) {
            if (i.getUserID().equals(s)) {
                Log.e(TAG, i.getPortraitUri());
                return new UserInfo(i.getUserID(), i.getName(), Uri.parse(i.getPortraitUri()));
            }
        }


        Log.e("MainActivity", "UserId is : " + s);
        return null;
    }
}
